package com.dcits.idea.window;

import com.dcits.idea.data.DataCenter;
import com.dcits.idea.processor.DefaultDataNode;
import com.dcits.idea.processor.FreemarkerProcessor;
import com.dcits.idea.processor.MDFreeMarkerProcess;
import com.dcits.idea.processor.Processor;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.Notifications;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @Author tangmqb DCITS
 * @DATE 2021/11/4 9:36
 * @Version 1.0.0
 */
public class NodeListWindow {
    private JTable contentTable;
    private JButton createBtn;
    private JButton clearBtn;
    private JButton closeBtn;
    private JTextField tfTopic;
    private JPanel jcontent;

    public NodeListWindow(Project project, ToolWindow toolWindow) {
        init();
        createBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String title = tfTopic.getText();
                if (title == null || "".equals(title)) {
//                    MessageDialogBuilder.yesNo("操作结果", "文档标题没有输入").show();
                    Messages.showMessageDialog(project,"笔记标题没有输入","操作结果",null);
                    return;
                }
                VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFileDescriptor(), project, project.getBaseDir());
                if (virtualFile != null) {
                    //获取要生成的文件名

                    String path = virtualFile.getPath();
                    String filePath = path+ File.separator+title+".md";
                    System.out.println("filePath:"+filePath);
                    Processor processor = new MDFreeMarkerProcess();
                    try {
                        processor.process(new DefaultDataNode(filePath,title,DataCenter.NOTE_LIST));
                        //提示
                        NotificationGroup notify = new NotificationGroup("mknote_id", NotificationDisplayType.BALLOON, true);
                        Notification nft = notify.createNotification("生成文档成功:"+filePath, MessageType.INFO);
                        Notifications.Bus.notify(nft);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }


                }

            }
        });
        clearBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DataCenter.reset();
            }
        });
        closeBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toolWindow.hide(null);
            }
        });
    }

    private void init() {
        contentTable.setModel(DataCenter.TABLE_MODEL);
        contentTable.setEnabled(true);
    }

    public JPanel getJcontent() {
        return jcontent;
    }
    private void createUIComponents() {
        // TODO: place custom component creation code here
    }
}
